package com.forum.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.enums.myOps1;
import com.forum.exception.myException;
import com.forum.mapper.ForumArticleMapper;
import com.forum.mapper.ForumCommentMapper;
import com.forum.mapper.UserMessageMapper;
import com.forum.pojo.ForumArticle;
import com.forum.pojo.ForumComment;
import com.forum.pojo.LikeRecord;
import com.forum.pojo.UserMessage;
import com.forum.service.LikeRecordService;
import com.forum.mapper.LikeRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.forum.enums.myOps1.ARTICLE_LIKE;

/**
 *
 */
@Service
public class LikeRecordServiceImpl extends ServiceImpl<LikeRecordMapper, LikeRecord>
        implements LikeRecordService {
    @Autowired
    private ForumArticleMapper forumArticleMapper;
    @Autowired
    private LikeRecordMapper likeRecordMapper;
    @Autowired
    private ForumCommentMapper forumCommentMapper;
    @Autowired
    private UserMessageMapper userMessageMapper;
    /**
     * 点赞功能
     * @param objectId
     * @param userId
     * @param opType
     */
    @Override
    @Transactional(rollbackFor = {Exception.class}, readOnly = false)
    public void doLike(String objectId, Long userId, myOps1 opType) {
        UserMessage userMessage = new UserMessage();
        userMessage.setCreateTime(new Date());
        LikeRecord likeRecord = null;
        switch (opType) {
            case ARTICLE_LIKE:
                likeRecord=this.articleLike(objectId, userId);
                ForumArticle forumArticle1 = (ForumArticle)this.forumArticleMapper.selectByArticleId(objectId);
                userMessage.setArticleId(objectId);
                userMessage.setArticleTitle(forumArticle1.getTitle());
                userMessage.setMessageType(2);
                userMessage.setCommentId(0);
                userMessage.setReceivedUserId(forumArticle1.getUserId());
                break;
            case COMMENT_LIKE:
                likeRecord=this.commentLike(objectId, userId);
                ForumComment forumComment2 = (ForumComment)this.forumCommentMapper.selectByCommentId(Integer.parseInt(objectId));
                ForumArticle forumArticle2 = (ForumArticle)this.forumArticleMapper.selectByArticleId(forumComment2.getArticleId());
                userMessage.setArticleId(forumArticle2.getArticleId());
                userMessage.setArticleTitle(forumArticle2.getTitle());
                userMessage.setMessageType(3);
                userMessage.setCommentId(Integer.parseInt(objectId));
                userMessage.setReceivedUserId(forumComment2.getUserId());
                userMessage.setMessageContent(forumComment2.getContent());
        }
        userMessage.setSendUserId(userId);
        userMessage.setSendNickName(forumArticleMapper.findNickNameByUserId(userId));
        userMessage.setStatus(0);
        if (likeRecord == null && !userId.equals(userMessage.getReceivedUserId())) {
            this.userMessageMapper.insert(userMessage);
        }
    }

    /**
     * 评论点赞
     * @param objectId
     * @param userId
     */
    private LikeRecord commentLike(String objectId,Long userId) {
        LikeRecord likeRecord = (LikeRecord) this.likeRecordMapper.selectByObjectIdAndUserIdAndOpType(objectId, userId, 1);
       //判断是否有点赞记录
        if (likeRecord != null) {
            this.likeRecordMapper.deleteByObjectIdAndUserIdAndOpType(objectId, userId, 1);
            this.forumCommentMapper.updateCommenCount(-1, Integer.parseInt(objectId));
            this.userMessageMapper.deleteByCommentIdAndUserId(objectId,userId);
            return likeRecord;
        } else {
            ForumComment forumComment = this.forumCommentMapper.selectByCommentId(Integer.parseInt(objectId));
            if (null == forumComment) {
                throw new myException("评论不存在");
            }
            LikeRecord likeRecord1 = new LikeRecord();
            likeRecord1.setObjectId(objectId);
            likeRecord1.setUserId(userId);
            likeRecord1.setOpType(1);
            likeRecord1.setCreateTime(new Date());
            likeRecord1.setAuthorUserId(forumComment.getUserId());
            this.likeRecordMapper.insert(likeRecord1);
            this.forumCommentMapper.updateCommenCount(1, Integer.parseInt(objectId));

        }
        return likeRecord;
    }

    /**
     * 文章点赞
     * @param objectId
     * @param userId
     */
    private LikeRecord articleLike(String objectId, Long userId) {
        LikeRecord likeRecord = (LikeRecord) this.likeRecordMapper.selectByObjectIdAndUserIdAndOpType(objectId, userId, 0);
        if (likeRecord != null) {
            this.likeRecordMapper.deleteByObjectIdAndUserIdAndOpType(objectId, userId, 0);
            this.userMessageMapper.deleteByArticleIdAndUserId(objectId,userId);
            this.forumArticleMapper.updateArticleCount(1, -1, objectId);
            return likeRecord;
        } else {
            ForumArticle forumArticle = (ForumArticle) this.forumArticleMapper.selectByArticleId(objectId);
            if (null == forumArticle) {
                throw new myException("文章不存在");
            }

            LikeRecord likeRecord1 = new LikeRecord();
            likeRecord1.setObjectId(objectId);
            likeRecord1.setUserId(userId);
            likeRecord1.setOpType(0);
            likeRecord1.setCreateTime(new Date());
            likeRecord1.setAuthorUserId(forumArticle.getUserId());
            this.likeRecordMapper.insert(likeRecord1);
            this.forumArticleMapper.updateArticleCount(1, 1, objectId);
        }
        return likeRecord;
    }
}




